package com.yanqu.road.server.gameplayer.module.passRoad;

import com.yanqu.road.entity.config.passroad.*;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserPassTransport;
import com.yanqu.road.logic.bussiness.player.UserPassTransportBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.pass.RoadTransportProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.*;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

/**
 * 玩家关卡（新）坐骑模块
 */
public class PassTransportModule extends GeneralModule {

	private Map<Integer, UserPassTransport> transportMap;

	public PassTransportModule(GamePlayer player) {
		super(player);
	}

	@Override
	public boolean loadData() {
		transportMap = UserPassTransportBussiness.getUserPassTransport(player.getUserId());
		return true;
	}

	@Override
	public boolean afterLoadData() {
		return true;
	}

	@Override
	public void afterLogin() {
		syncUserPassRoadTransport();
	}

	@Override
	public void loginSendMsg() {

	}

	@Override
	public boolean saveData() {
		for(UserPassTransport data : transportMap.values()){
			if(data.isInsertOption()){
				UserPassTransportBussiness.addUserPassTransport(data);
			}else if(data.isUpdateOption()){
				UserPassTransportBussiness.updateUserPassTransport(data);
			}
		}
		return true;
	}

	//版本更新已经解锁了关卡的老玩家获取驴和小船,身份达到18牛车，vip达到3马车,vip达到5轿车
	public void addVersionUpdateTransport(){
		Property faChe = new Property(PropertyHelper.parseStringToProperty(GameConfig.PASS_ROAD_TRANSPORT_UPDATE_VERSION));
		String niuChe = "108001=1";
		String maChe = "108002=1";
		String jiaoChe = "108003=1";
		if(player.getUserInfo().getTitleId() >= 18){
			faChe.addProperty(PropertyHelper.parseStringToProperty(niuChe));
		}
		if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) >= 3){
			faChe.addProperty(PropertyHelper.parseStringToProperty(maChe));
		}
		if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) >= 5){
			faChe.addProperty(PropertyHelper.parseStringToProperty(jiaoChe));
		}
		player.getModule(CurrencyModule.class).addCurrency(faChe,eLogMoneyType.Transport,eLogMoneyType.TransportVersionUpdate);
	}

	public UserPassTransport addTransport(int tpId, eLogMoneyType master, eLogMoneyType son) {
		PassRoadTransportInfo info = PassRoadMgr.getTransportInfo(tpId);
		UserPassTransport userPassTransport = null;
		if (null != info) {
			if (!transportMap.containsKey(tpId)) {
				userPassTransport = new UserPassTransport();
				userPassTransport.setUserId(player.getUserId());
				userPassTransport.setTemplateId(info.getId());
				userPassTransport.setCreateTime(System.currentTimeMillis());
				userPassTransport.setInsertOption();

				transportMap.put(userPassTransport.getTemplateId(),userPassTransport);
				LogMgr.addLogGetTransport(player.getUserId(),tpId, master.getValue(), son.getValue(),1);
			}
		}
		syncUserPassRoadTransport();
		return userPassTransport;
	}

	//金币消耗减少
	public int getTransportSilverAddition(){
		int addtion = 0;
		for(UserPassTransport transport : transportMap.values()){
			PassRoadTransportInfo transportInfo = PassRoadMgr.getTransportInfo(transport.getTemplateId());
			addtion += transportInfo.getEffectPar2();
		}
		return addtion;
	}

	//阅历加成
	public int getTransportExpAddition(){
		int addtion = 0;
		for(UserPassTransport transport : transportMap.values()){
			PassRoadTransportInfo transportInfo = PassRoadMgr.getTransportInfo(transport.getTemplateId());
			addtion += transportInfo.getEffectPar1();
		}
		return addtion;
	}

	public int loadTransport(int tpId){
		if(!transportMap.containsKey(tpId)){
			return GameErrorCode.E_PASS_ROAD_TRANSPORT_NOFOUND;
		}
		UserPassTransport tp = transportMap.get(tpId);
		player.getModule(PassRoadModule.class).getUserPassRoad().setTransportId(tp.getTemplateId());
		return 0;
	}

	private void syncUserPassRoadTransport(){
		RoadTransportProto.PassTransportSyncMsg.Builder userMsg = RoadTransportProto.PassTransportSyncMsg.newBuilder();
		for(UserPassTransport transport : transportMap.values()){
			userMsg.addTransportId(transport.getTemplateId());
		}
		player.sendPacket(Protocol.U_PASS_ROAD_TRANSPORT_SYNC, userMsg);
	}
}
